package com.wink_172.library.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.databinding.DataBindingUtil
import com.hjq.language.MultiLanguages
import com.wink_172.library.AppManager
import com.wink_172.library.R
import com.wink_172.library.databinding.ActivityLanguageBinding
import com.wink_172.library.model.BaseDataItem
import com.wink_172.library.model.Constants
import com.wink_172.library.view.ItemView
import com.wink_172.library.view.ActionBarView
import java.util.*

class LanguageActivity : SmartActivity(), View.OnClickListener {
    lateinit var binding: ActivityLanguageBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_language)

        setStatusBarLightMode()
        initView()
        init()
    }

    private fun init() {
        val locale = MultiLanguages.getAppLanguage()
        binding?.let {
            binding->
            if (locale.displayLanguage.contains("中文")) {
                (binding.Simplified as ItemView).updateActive(true)
                (binding.English as ItemView).updateActive(false)
            } else if (locale.displayLanguage.contains("English")) {
                (binding.Simplified as ItemView).updateActive(false)
                (binding.English as ItemView).updateActive(true)
            } else {
                val locale2 = MultiLanguages.getSystemLanguage()
                if (locale2.displayLanguage.contains("中文")) {
                    (binding.Simplified as ItemView).updateActive(true)
                    (binding.English as ItemView).updateActive(false)
                } else {
                    (binding.Simplified as ItemView).updateActive(false)
                    (binding.English as ItemView).updateActive(true)
                }
            }
        }

        Log.e(TAG, "init: ====>>" + locale.displayLanguage)
    }

    override fun onClick(v: View) {
        val active: Boolean
        if (v.id == R.id.Simplified) {
            active = (binding!!.Simplified as ItemView).isCheck
            if (!active) {
                (binding!!.Simplified as ItemView).updateActive(true)
                (binding!!.English as ItemView).updateActive(false)
                eventChange(SIMPLIFIED_CHINESE)
            }
        } else if (v.id == R.id.English) {
            active = (binding!!.English as ItemView).isCheck
            if (!active) {
                (binding!!.Simplified as ItemView).updateActive(false)
                (binding!!.English as ItemView).updateActive(true)
                eventChange(ENGLISH)
            }
        }
    }

    private fun eventChange(pos: Int) {
        when (pos) {
            SIMPLIFIED_CHINESE -> MultiLanguages.setAppLanguage(this, Locale.SIMPLIFIED_CHINESE) //设置简体中文
            ENGLISH -> MultiLanguages.setAppLanguage(this, Locale.ENGLISH) //设置英文
            TRADITIONAL_CHINESE -> MultiLanguages.setAppLanguage(this, Locale.TRADITIONAL_CHINESE) //设置繁体中文
        }



        val intent = Intent()
        intent.putExtra(Constants.PARAM_DATA1, pos)
        setResult(RESULT_OK, intent) //调用者在activityresult接受结果回调

        AppManager.appManager?.finishActivity(this);
    }

    private fun initView() {

        (binding!!.actionBar as ActionBarView).updateAllContent(
            R.mipmap.icon_back,
            "kotlin语言",
            ""
        );

        (binding!!.actionBar as ActionBarView).setCallback(object :
            ActionBarView.ICallback {
            override fun onSendEvent(event: Int, content: String?) {
                if (event == ActionBarView.LEFT_BTN) {
                    finish()
                }
            }


        })


        val titleItem1 = BaseDataItem()
        titleItem1.content = "简体中文"
        (binding!!.Simplified as ItemView).updateContent(titleItem1)
        val titleItem2 = BaseDataItem()
        titleItem2.content = "英文"
        (binding!!.English as ItemView).updateContent(titleItem2)
    }

    companion object {
        fun startActivity(context: Activity, vararg args: Any) {
            val intent = Intent(context, LanguageActivity::class.java).apply {
                //    putExtra(Constants.PARAM_DATA1, (String) args[0]);
            }


            context.startActivityForResult(intent, args[0] as Int)
        }

        const val SIMPLIFIED_CHINESE = 0
        const val ENGLISH = 1
        const val TRADITIONAL_CHINESE = 2
        private const val TAG = "TAG"
    }
}